package cn.gary.oaserver.controller;


import cn.gary.oaserver.params.MenuParams;
import cn.gary.oaserver.params.Result;
import cn.gary.oaserver.service.ITMenuRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gary
 * @since 2024-03-15
 */
@RestController
@RequestMapping("/menuRole")
@Api(tags = "角色菜单中间表")
public class TMenuRoleController {

    @Autowired
    ITMenuRoleService menuRoleService;

    @ApiOperation("根据角色id和菜单的id去删除对应的关系表")
    @GetMapping("/delMenuChildrenByRoleId/{roleId}/{menuId}")
    public Result delMenuChildrenByRoleId(@PathVariable("roleId") Integer roleId,
                                          @PathVariable("menuId") Integer menuId) {
        if (menuRoleService.delMenuChildrenByRoleId ( roleId, menuId )) {
            return Result.success ( "删除权限成功" );
        } else {
            return Result.error ( "删除权限失败" );
        }
    }

    @ApiOperation("获取权限信息")
    @GetMapping("/getRoleMenu")
    public Result getRoleMenu() {
        return Result.success ( "权限信息", menuRoleService.getRoleMenu ( ) );
    }


    @ApiOperation("根据角色id获取对应权限信息")
    @GetMapping("/getRoleMenuById/{roleId}")
    public Result getRoleMenuById(@PathVariable("roleId") Integer roleId) {
     List<Integer> list= Arrays.asList ( menuRoleService.getRoleMenuById ( roleId ) );
        return Result.success ( "用户的权限信息", list );
    }

    @ApiOperation("添加选择的权限")
    @PostMapping("/addMenuRoles")
    public Result addMenuRoles(@RequestBody MenuParams menuParams) {
        if (menuRoleService.insetMenuRole ( menuParams )) {
            return Result.success ( "分配成功" );
        } else {
            return Result.error ( "分配失败" );
        }
    }
}
